package com.fresh.api;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fresh.common.annotation.IgnoreAuth;
import com.fresh.entity.AntifraudUser;
import com.fresh.entity.BlackEntity;
import com.fresh.entity.ExternalDataEntity;
import com.fresh.entity.ExternalSourceEntity;
import com.fresh.entity.JDEntity;
import com.fresh.entity.NumberUtils;
import com.fresh.entity.ShopEntity;
import com.fresh.entity.TdRiskReturn;
import com.fresh.entity.UserEntity;
import com.fresh.service.IExternalDataService;
import com.fresh.service.IExternalSourceService;
import com.fresh.service.IShopService;
import com.fresh.service.VerifyService;
import com.fresh.utils.Constant;
import com.fresh.utils.PageModel;
import com.fresh.vo.OrderInfoVo;
import com.google.gson.Gson;

/**
 * 用户登录
 *
 * @author alex
 * @email gaonan.service@gmail.com
 * @date 2017-03-23 15:31
 */
@RequestMapping("/External")
@Controller
public class ExternalController extends BaseController {

	private static final Logger log = LoggerFactory
			.getLogger(ExternalController.class);

	@Autowired
	private IExternalDataService externalDataService;

	@Autowired
	private IShopService shopService;

	@Autowired
	private IExternalSourceService externalSourceService;
	
	@Autowired
	private VerifyService verifyService;

	/**
	 * 
	 * @param 进初始页面
	 *            测试
	 * @return
	 */
	@IgnoreAuth
	@RequestMapping(value = "getOrderInfo", method = RequestMethod.GET)
	public String getOrderInfo() {
		return "/antiFraud";
	}

	/**
	 * 
	 * @param 订单详情分页
	 * @return
	 */
	@IgnoreAuth
	@RequestMapping(value = "getOrderPage")
	public String getOrderPage(HttpServletRequest request, String userId) {
		log.info("进入订单详情分页");
		// 当前页码和显示条数
		int currentPage = 1, size = 10;
		// 页面上的页码
		String pageNo = request.getParameter("pageNo");

		if (pageNo != null && !"".equals(pageNo)) {
			currentPage = Integer.parseInt(pageNo);
		}
		// 调用业务层方法
		PageModel<ExternalDataEntity> pm = externalDataService.queryOrderPage(
				currentPage, size, userId);
		request.setAttribute("pm", pm);
		request.setAttribute("userId", userId);

		System.out.println(pm.getSumCount());
		return "/orderInfo/queryOrder";
	}

	/**
	 * 
	 * @param 进订单详情
	 *            没用
	 * @return
	 */
	@IgnoreAuth
	@RequestMapping(value = "getOrder")
	public String getOrder(HttpServletRequest request, String userId) {

		List<OrderInfoVo> orderListVo = new ArrayList<OrderInfoVo>();

		// 当前员工的查询记录
		List<ExternalDataEntity> externalDataList = externalDataService
				.getExternalData(userId);

		for (int i = 0; i < externalDataList.size(); i++) {
			// 得到用户信息
			UserEntity user = externalDataService.queryById(externalDataList
					.get(0).getUserid());
			OrderInfoVo vo = new OrderInfoVo();
			vo.setName(user.getName());
			// 产品
			vo.setServiceType(externalDataList.get(i).getServiceType());
			vo.setExternalDataId(externalDataList.get(i).getId());
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

			vo.setCreatetime(df.format(externalDataList.get(i).getCreatetime()));
			orderListVo.add(vo);
		}
		// 得到公司信息
		request.setAttribute("orderListVo", orderListVo);
		return "/queryOrder";
	}

	/**
	 * 
	 * @param 进反欺诈查询报告
	 * @return
	 */
	@IgnoreAuth
	@RequestMapping(value = "queryOrderInfo")
	public String queryOrderInfo(HttpServletRequest request, String id) {
		log.info("进反欺诈查询报告");
		Gson gson = new Gson();
		// 反欺诈单条详情
		ExternalDataEntity externalData = externalDataService.queryExternal(id);
		List<ExternalSourceEntity> externalSourceList = externalSourceService
				.getExternalSource(id);

		// 借贷
		JDEntity sMJDDetail = new JDEntity();
		// 黑名单
		BlackEntity sMHMDDetail = new BlackEntity();
		//同盾
		TdRiskReturn tdRiskReturn = new TdRiskReturn();
		

		for (int i = 0; i < externalSourceList.size(); i++) {
			String dataSource = externalSourceList.get(i).getDatasource();
			if (externalSourceList.get(i).getIntertype() == 1) {
				sMJDDetail = gson.fromJson(dataSource, JDEntity.class);
			}

			if (externalSourceList.get(i).getIntertype() == 2) {
				sMHMDDetail = gson.fromJson(dataSource, BlackEntity.class);
			}
			
			if (externalSourceList.get(i).getType() == 1) {
				tdRiskReturn = gson.fromJson(dataSource, TdRiskReturn.class);
			}
		}
		
		AntifraudUser au = new AntifraudUser();
		au.setName(externalData.getName());
		au.setCardNumber(externalData.getCardnumber());
		au.setPhone(externalData.getPhone());
		au.setId(id);
		au.setDivision("3");
		// 调公用方法
		AntifraudController AntifraudController=new AntifraudController();
		AntifraudController.setRequest(sMJDDetail, request, sMHMDDetail, au,0,tdRiskReturn);
	
		// 判断页面不显示 增值服务和免责声明
		request.setAttribute("orderType", "1");
		return "/buyer/queryReport";
	}

	/**
	 * 
	 * @param 删除订单
	 * @return
	 * @throws IOException 
	 */
	@IgnoreAuth
	@RequestMapping(value = "updateOrderInfo")
	public String updateOrderInfo(HttpServletRequest request, String userId,HttpServletResponse response) throws IOException {
		log.info("进入删除订单");
		Boolean boo = externalDataService.updateUserById(userId);

		if (boo) {

			return getOrderInfoPage(request,response);
		} else {

			return null;
		}

	}

	/**
	 * 
	 * @param 订单信息
	 *            没用
	 * @return
	 */
	@IgnoreAuth
	@RequestMapping(value = "getOrderInfoList")
	public String getOrderInfoList(HttpServletRequest request) {

		List<OrderInfoVo> orderListVo = new ArrayList<OrderInfoVo>();
		// 得到商户下的用户信息
		List<UserEntity> userList = externalDataService
				.getUser(getUserId(request));
		for (int i = 0; i < userList.size(); i++) {
			// 单个员工的查询次数
			int count = externalDataService.getExternalDataCount(userList
					.get(i).getId());
			List<ExternalDataEntity> externalDataList = externalDataService
					.getExternalData(userList.get(i).getId());
			OrderInfoVo vo = new OrderInfoVo();
			vo.setId(Integer.parseInt(userList.get(i).getId()));
			vo.setName(userList.get(i).getName());
			vo.setCount(count);
			if (externalDataList.size() > 0) {
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				vo.setCreatetime(df.format(externalDataList.get(0)
						.getCreatetime()));
			} else {
				vo.setCreatetime("");
			}
			orderListVo.add(vo);
		}
		ShopEntity shop = shopService.getShopSearchNumber(getUserId(request));
		// 得到公司信息
		request.setAttribute("orderListVo", orderListVo);
		return "/orderManagement";
	}

	/**
	 * 
	 * @param 订单信息Page
	 * @return
	 * @throws IOException 
	 */
	@IgnoreAuth
	@RequestMapping(value = "getOrderInfoPage")
	public String getOrderInfoPage(HttpServletRequest request,HttpServletResponse response) throws IOException {
		log.info("进入订单信息分页  userId:{} ", getUserId(request));

		//权限拦截
		verifyService.permissionIntercept(Constant.ORDERINFOPAGE_PER, response, request);
		
		// 当前页码和显示条数
		int currentPage = 1, size = 8;
		// 页面上的页码
		String pageNo = request.getParameter("pageNo");
		if (pageNo != null && !"".equals(pageNo)) {
			currentPage = Integer.parseInt(pageNo);
		}

		PageModel<ExternalDataEntity> pm = externalDataService.queryPage(
				currentPage, size, getShopId(request));
		// 得到商户信息
		ShopEntity shop = shopService.getShopSearchNumber(getShopId(request));
		// 商户的已使用条数
		shop.setBalance(NumberUtils.formatMoney(shop.getBalance(), 2));
		request.setAttribute("shop", shop);
		request.setAttribute("pm", pm);

		return "/orderInfo/orderManagement";
	}

}
